// pages/home/index.js
import { getReqBanData, getReqCatory, getReqFloot, getReqRecommend } from '../../api/homeReq'
import { reqMenu } from '../../api/searchReq'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    //input框是否聚焦
    focus: false,
    val: "",
    //是否显示
    showSearch: true,
    //轮播图初始数据
    swiperImgData: [
      { id: 1, imgSrc: "https://z3.ax1x.com/2021/08/28/h1hZzn.png" },
      { id: 2, imgSrc: "https://z3.ax1x.com/2021/08/28/h1hZzn.png" },
      { id: 3, imgSrc: "https://z3.ax1x.com/2021/08/28/h1hZzn.png" },
    ],
    tempNum1: [
      { id: 1, imgSrc: "https://z3.ax1x.com/2021/08/28/h31aGR.png" },
      { id: 2, imgSrc: "https://z3.ax1x.com/2021/08/28/h31aGR.png" },
      { id: 3, imgSrc: "https://z3.ax1x.com/2021/08/28/h31aGR.png" },
      { id: 4, imgSrc: "https://z3.ax1x.com/2021/08/28/h31aGR.png" },
    ],
    categroy: [
      { id: 1, imgSrc: "../../assets/images/icons/jiachangcia588@3x.png" },
      { id: 2, imgSrc: "../../assets/images/icons/jiachangcia588@3x.png" },
      { id: 3, imgSrc: "../../assets/images/icons/jiachangcia588@3x.png" },
      { id: 5, imgSrc: "../../assets/images/icons/jiachangcia588@3x.png" },
      { id: 6, imgSrc: "../../assets/images/icons/jiachangcia588@3x.png" },
      { id: 7, imgSrc: "../../assets/images/icons/jiachangcia588@3x.png" },
      { id: 8, imgSrc: "../../assets/images/icons/jiachangcia588@3x.png" },
      { id: 4, imgSrc: "../../assets/images/icons/jiachangcia588@3x.png" },
      { id: 9, imgSrc: "../../assets/images/icons/jiachangcia588@3x.png" },
    ],
    floot: [],//第一层楼层
    searchList: [
      { _id: 1, name: "暂无数据" },

    ],
    tempList: [],
    recommend: [],//推荐
    total: 0//总共菜品

  },

  goToCatory() {
    wx.navigateTo({
      url: '../category/category',
    })
  },
  //输入框聚焦时触发
  focusFoo(event) {
    this.setData({
      showSearch: false,
      tempList: this.data.searchList
    })
  },
  //输入框失焦的时候
  bindblur() {
    this.setData({
      showSearch: true,
      val: ""
    })
  },
  //输入框输入的时候触发
  time: "", //用来做防抖
  bindinput(event) {
    if (!this.time) {
      this.time = setTimeout(() => {
        this.getSearchData({ val: this.data.val })
      }, 1000);
    } else {
      clearTimeout(this.time)
      this.time = setTimeout(() => {
        this.getSearchData({ val: this.data.val })
      }, 1000);
    }
    // console.log(event.detail.value.trim(), this.data.val);
  },
  //点击搜索的时候跳转到搜索的页面
  gotoSearchPage(e) {
    const id = e.mark.id;
    console.log(id);
    if (id !== 1) {
      wx.navigateTo({
        url: `/pages/home/detail/detail?id=${id}`
      })
    }

  },
  gotoSearch(e) {
    console.log(e.mark.name);
    if (e.mark.name == "分类") {
      wx.navigateTo({
        url: "../category/category"
      })
      return
    }
    wx.navigateTo({
      url: `../search/search?name=${e.mark.name}`
    })
  },
  //输入内容得到对应的内容
  async getSearchData(data = {}) {
    const res = await reqMenu(data)
    if (res.meta.status == 201) {
      this.setData({
        tempList: [{ _id: 1, name: "暂无内容", click: true }]
      })
    } else if (res.meta.status == 200) {
      this.setData({
        tempList: res.menus
      })
    }
    console.log(res);
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },
  async getBanData() {
    const res = await getReqBanData()//获取轮播图
    const catory = await getReqCatory()//获取分类图
    let floot = await getReqFloot()//得到底层
    floot = floot.message.map(item => {
      item.floor_title = item.floor_title.replace(/\"/g, "")
      return item
    })
    const recommend = await getReqRecommend()//得到推荐商品
    console.log(floot);
    const res1 = await reqMenu()//得到所有的菜品
    this.setData({
      swiperImgData: res.message,
      categroy: catory.message,
      floot,
      recommend: recommend.message,
      searchList: recommend.message,
      total: res1.total

    })

    // console.log(res);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.getBanData()

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      showSearch: true,
      val: ""
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})